using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinCE
{
    /// <summary>
    /// 
    /// </summary>
	public class Dialogs
	{
		private Dialogs()
		{
		}

		private static IntPtr GetHwnd(Control control)
		{
			control.Capture = true;
			IntPtr hwnd = GetCapture();
			control.Capture = false;

			return hwnd;
		}

		[ DllImport("coredll.dll") ]
		private static extern IntPtr GetCapture();

		[DllImport("coredll.dll")]
		private static extern bool SetForegroundWindow(IntPtr hwnd);

		//
		// Activate
		//
		// Activates and brings a specific form to the front.
		//
        /// <summary>
        /// Activates the specified form.
        /// </summary>
        /// <param name="form">The form.</param>
		public static void Activate(Form form)
		{
            //form.Capture = true;
            //IntPtr hwnd = GetCapture();
            //form.Capture = false;                     
			// SetForegroundWindow(hwnd);

            form.Activate();
		}

		private static Form m_top;

		//
		// ShowDialog
		//
		// This method makes ShowDialog work the way I want. Here is what it does:
		//
		//	1.	Sets the caption of the new window to the same as the caller's.
		//	2.	Clears the caption of the parent so it won't show up in the task list.
		//	3.	When the ShowDialog call returns, brings the previous window
		//		back to the foreground.
		//
        /// <summary>
        /// Shows the dialog.
        /// </summary>
        /// <param name="dialog">The dialog.</param>
        /// <param name="parent">The parent.</param>
		public static void ShowDialog(Form dialog, Control parent)
		{
			Control top = parent.TopLevelControl;
			string caption = top.Text;
			dialog.Text = caption;
			top.Text = "";							// Don't show parent in task list

			dialog.Activated += new EventHandler(form_Activated);
			dialog.Closed += new EventHandler(form_Closed);
			m_top = dialog;							// New top-most form
			dialog.ShowDialog();

			m_top = (Form) top;						// The top dialog just changed
			dialog.Activated -= new EventHandler(form_Activated);
			dialog.Closed -= new EventHandler(form_Closed);

			top.Text = caption;						// Make visible in task list again
            ((Form)top).Visible=true;
            ((Form)top).Activate(); 				// And make it the active window
            ((Form)top).Refresh();            
		}

		//
		// form_Activated
		//
		// If one of our other windows, such as the main window, receives an activate event,
		// will activate the current top-most window instead.
		//
		private static void form_Activated(object sender, EventArgs e)
		{
			if (m_top != null && !(sender == m_top))	// Is this the top-most window?
				Activate(m_top);						// No, activate the top dialog
		}

		private static void form_Closed(object sender, EventArgs e)
		{
			m_top = null;								// When you close the top dialog, it's not top anymore
		}
	}
}
